package com.hjw.weixun.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hjw.weixun.domain.User;
import com.hjw.weixun.domain.Weixun;
import com.hjw.weixun.service.UserService;
import com.hjw.weixun.service.WeixunService;
import com.hjw.weixun.service.impl.UserServiceImpl;
import com.hjw.weixun.service.impl.WeixunServiceImpl;

/**
 * 用户信息管理
 * 
 * @author wangchangxin date 2011-8-8 RightAction
 * 
 */
public class RightAction extends GenericAction {

	private static final long serialVersionUID = 7249804172349063600L;

	private static final Logger logger = LoggerFactory
			.getLogger(RightAction.class);

	private UserService userService;

	private WeixunService weixunService;

	private User user;

	private Weixun weixun;

	private String path;

	// 无参构造函数
	public RightAction() {

		userService = UserServiceImpl.getInstance();

		weixunService = WeixunServiceImpl.getInstance();
	}

	/**
	 * 登录方法
	 * 
	 * @return
	 */
	public String login() {
		path = "index.jsp";

		// 判断页面是否传过来用户参数
		if (null != user) {
			user = userService.getUser(user);

			// 判断
			if (null != user) {
				super.getRequest().getSession().setAttribute("user", user);
				renderHtml("{\"success\":\"true\"}");
				return null;
			}
		}
		renderHtml("{\"success\":\"false\"}");
		return null;
	}

	/**
	 * 获取Seesion中的值 转为Json类型
	 * 
	 * @return
	 */
	public String getSessionValue() {

		String key = super.getRequest().getParameter("key");

		// 验证码
		String value = (String) super.getRequest().getSession()
				.getAttribute(key);

		renderHtml("{\"" + key + "\":\"" + value + "\"}");
		return null;

	}

	/**
	 * 注册功能
	 * 
	 * @return
	 */
	public String register() {

		if (null != user) {

			// 持久化到数据库
			userService.addUser(user);

			// 更新到Session中
			super.getRequest().getSession().setAttribute("user", user);
			renderHtml("{\"success\":\"true\"}");
		}

		return null;
	}

	/**
	 * 创建微讯账户
	 * 
	 * @return
	 */
	public String create() {
		if (null != weixun  && null!= (user = super.getCurrentUser())) {
			weixun.setUser(user);
			
			Integer weixunid = weixunService.addWeixun(weixun);
			renderHtml("{\"success\":\"true\",\"weixunid\":\"" + weixunid
					+ "\"}");
		}

		return null;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Weixun getWeixun() {
		return weixun;
	}

	public void setWeixun(Weixun weixun) {
		this.weixun = weixun;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

}
